package life.majiang.community.strategy;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import life.majiang.community.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Map;

@Service
public class StudyUserStrategy implements UserStrategy{

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public LoginUserInfo getUser(String code, String state) {
        LoginUserInfo userInfo =null;
        Map<String,Object> tokenMap = TokenUtil.analysisToken(code);
        Object obj = redisTemplate.opsForValue().get("studentid:"+tokenMap.get("id"));
        if(obj!=null && !"".equals(obj+"")){
            //判断当前用户是否被挤下线
            JSONObject loginUser = JSON.parseObject(obj+"");
            String Ttoken = (String)tokenMap.get("Ttoken");
            if(loginUser.get("ttoken").equals(Ttoken)){
                userInfo = new LoginUserInfo();
                JSONObject object = JSON.parseObject(obj+"");
                userInfo.setId(Long.parseLong(object.get("id")+""));
                userInfo.setAvatarUrl(object.get("headimg")+"");
                userInfo.setName(object.get("lastname")+"");
            }
        }
        return userInfo;
    }

    @Override
    public String getSupportedType() {
        return "study";
    }
}
